package localAI.graphics;

import java.applet.Applet;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import localAI.env.def.Orientation;

/**
 * 
 * @author Isaac
 *
 * This class take care of the visual.
 */

public class Boom extends Applet
{
	public Icon iconAN,iconAW,iconAE,iconANRed,iconAWRed,iconAERed,icon6,
		   icon7,iconv,iconb,iconflag,iconb_2,iconboomEror;
	public String pict[] ={"AgentNorth.GIF","AgentSouth.GIF","AgentWest.GIF","AgentEast.GIF","AgentNorthRed.GIF",
			"AgentSouthRed.GIF","AgentWestRed.GIF","AgentEastRed.GIF","view.jpg","wall.jpg","flag.jpg"};
	public JPanel p;
	public JLabel[] b;

//************************************************************************************

/**
 * Create a board and initialize it.
 */	
public void init(int Xsize,int Ysize)
{	
	iconv = new ImageIcon(pict[8]);
	iconb = new ImageIcon(pict[9]);
	iconflag = new ImageIcon(pict[10]);
	
	p = new JPanel();
	p.setLayout(new GridLayout(Ysize,Xsize));
	b = new JLabel[Ysize*Xsize];
	for(int j=0;j<(Ysize*Xsize);j++)
	{
		b[j] = new JLabel(iconv);
		b[j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		p.add(b[j]);
	}

	for(int j=0;j<Xsize;j++)
		b[j].setIcon(iconb);
	
	for(int j=Xsize;j<((Ysize*Xsize)-Xsize);j+=Xsize){
		b[j].setIcon(iconb);
		b[j+Xsize-1].setIcon(iconb);
	}
	for(int j=((Ysize*Xsize)-Xsize);j<(Ysize*Xsize);j++){
		b[j].setIcon(iconb);
	}
	setLayout(new GridLayout());
	add(p);
}
//***********************************************
/* Sets a Wall */
public void setWall(int cell){	
    b[cell].setIcon(iconb);
}
//*************************************************
/* Sets Flags */
public void setFlag(int cell){
	b[cell].setIcon(iconflag);
}
/* delete the content of a cell */
public void deleteContent(int cell){
	b[cell].setIcon(iconv);
}


//********************************************************************
	/* Initialize the agent location  */	
	public void setAgent(SingleAgent agent) {	
		int offset =findIconOffset((String)(agent.getColor()),agent.getOrientation()); 	
		b[agent.getLocation()].setIcon(new ImageIcon(pict[offset]));	
	}

	private int findIconOffset (String color,Orientation orientation){
		int offset = 0;
		if(color.compareTo("Red") == 0)
			offset = 4;
		if (orientation.equals(Orientation.South))
			offset += 1;
		else if (orientation.equals(Orientation.West))
			offset += 2;
		else if (orientation.equals(Orientation.East))
			offset += 3;
		return offset;
	}
}



